package com.jic.member.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.common.redis.RedisClientTemplate;
import com.jic.enums.RedisKey;
import com.jic.member.entity.MememberIntegralLog;
import com.jic.member.entity.MememberIntegralRule;
import com.jic.member.entity.MememberUser;
import com.jic.member.request.*;
import com.jic.member.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/rest/integral")
@Api(tags = {"会员积分接口"})
@Slf4j
public class MememberIntegralOperationController {


    @Resource
    private MememberIntegralLogService mememberIntegralLogService;

    @Resource
    private MememberIntegralRuleService mememberIntegralRuleService;

    @Resource
    private MememberActionNewService mememberActionNewService;


    @Autowired
    RedisClientTemplate redisClientTemplate;


    /**
     * 查询会员积分详情
     *
     * @param memberIntegralInfo
     * @return
     */
    @PostMapping("/getMememberIntegralInfo")
    public RestResult getMemberIntegralInfo(@RequestBody MememberIntegralRequest memberIntegralInfo) {
        log.info("【卡奥斯对外接口】【查询会员积分详情】=========》,{}", memberIntegralInfo.toString());
        return mememberActionNewService.getMemberIntegralInfo(memberIntegralInfo);
    }

    /**
     * 新增/消耗会员积分操作
     * @param request 外部接口请求参数
     * @return 响应码，响应信息，数据
     */
    @PostMapping("/addMemberIntegral")
    RestResult addMemberIntegralInfoNew(@RequestBody MemberIntegralRestRequest request){
        return mememberActionNewService.addMemberIntegralInfoNew(request);
    }

    /**
     *
     *修改会员积分接口
     * @param MememberIntegralActionRequest
     * @return 返回结果；
     * @tzc
     * @date 2020-6-10 21:54:44
     */
    @PostMapping("/updateMemberIntegralInfo")
    public RestResult updateMemberIntegralInfo(@RequestBody MememberIntegralActionRequest MememberIntegralActionRequest) {
        return mememberActionNewService.updateMemberIntegralInfo(MememberIntegralActionRequest);
    }

    /**
     * @param form
     * @return
     */
    @ApiOperation(value = "查询当前会员积分流水列表")
    @ResponseBody
    @RequestMapping(value = "/queryMemberIntegralList")
    public RestResult<PageResult<MememberIntegralLog>> queryMemberIntegralList(@RequestBody MememberIntegralListRequest form) {

        String token = form.getToken();
        String memStr = redisClientTemplate.get(RedisKey.H5_LOGIN_TOKEN_.getName() + token);
        MememberUser member = JSONArray.parseObject(memStr, MememberUser.class);

        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberIntegralLog integralLog = new MememberIntegralLog();
        integralLog.setMemberUserId(member.getId());
        PageResult<MememberIntegralLog> result = mememberIntegralLogService.queryPage(integralLog, page);
        return RestResult.success(result);
    }

    /**
     * @param form
     * @return
     */

    @ApiOperation(value = "新增积分流水记录")
    @ResponseBody
    @RequestMapping(value = "/addMemberIntegralLog")
    public RestResult addMemberIntegralLog(@RequestBody MememberIntegralLogQueryRequest form) {
        log.info("开始进入新增积分流水=========》,{}", form.toString());
        MememberIntegralLog integralLog = new MememberIntegralLog();
        integralLog.setThirdUserId(form.getThirdUserId());
        return RestResult.success(mememberIntegralLogService.insert(integralLog));
    }

    /**
     * @param form
     * @return
     */

    @ApiOperation(value = "修改积分流水记录")
    @ResponseBody
    @RequestMapping(value = "/updateMemberIntegralLog")
    public RestResult updateMemberIntegralLog(@RequestBody MememberIntegralLogQueryRequest form) {
        log.info("开始进入修改积分流水=========》,{}", form.toString());
        MememberIntegralLog integralLog = new MememberIntegralLog();
        integralLog.setThirdUserId(form.getThirdUserId());
        return RestResult.success(mememberIntegralLogService.update(integralLog));
    }

    /**
     * @param form
     * @return
     */

    @ApiOperation(value = "查询平台积分抵现规则")
    @ResponseBody
    @RequestMapping(value = "/queryIntergralRuleList")
    public RestResult<PageResult<MememberIntegralRule>> queryIntergralRuleList(@RequestBody MememberIntegralActionRequest form) {
        log.info("查询平台积分抵现规则=========》,{}", form.toString());
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberIntegralRule params = new MememberIntegralRule();
        params.setDeptId(form.getDeptId());
        PageResult<MememberIntegralRule> result = mememberIntegralRuleService.queryPage(params, page);
        return RestResult.success(result);
    }

}
